package com.itheima.demo;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.User;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

/**
 * 文档的CRUD
 * @作者 itcast
 * @创建日期 2020/11/7 8:54
 **/
public class EsDemo02 {
    RestHighLevelClient client;

    @Test
    public  void addDocument() throws IOException {
        // 要添加的一条文档数据
        User user = new User(1L,"鲁班",20,"1","鲁班在河道唱着歌");
//        1. 创建一个添加文档的请求  (IndexRequest)
        IndexRequest request = new IndexRequest("user");
//        索引库的设置
//        指定文档ID
        request.id(user.getId()+"");
//        文档的内容
        String userJson = JSON.toJSONString(user);// 将对象序列化成json字符串
        request.source(userJson,XContentType.JSON);
//        2. 客户端执行请求 .index()
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
//        3. 解析响应结果
        System.out.println(response.getResult()); // result : created  updated deleted
    }
    @Test
    public void findById() throws IOException {
//        1. 创建查看文档请求对象 (GetRequest)
        GetRequest request = new GetRequest("user","1");
//        2. 客户端执行对应方法 .get
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
//        3. 解析响应结果  _source
        String userJson = response.getSourceAsString();

        User user = JSON.parseObject(userJson, User.class);

        System.out.println(user);
    }
    @Test
    public void updateDocument() throws IOException {
//        1. 创建修改文档请求对象 (UpdateRequest)
        UpdateRequest request = new UpdateRequest("user","1");
//        可以只更改某个字段
        // field  对应字段的值
        request.doc("gender","0");
//        2. 客户端执行对应方法 .update
        UpdateResponse update = client.update(request, RequestOptions.DEFAULT);
//        3. 解析响应结果
        System.out.println(update.getResult());
    }
    @Test
    public void deleteDocumentById() throws IOException {
        // 1. 创建删除文档请求 (DeleteRequest)
        DeleteRequest request = new DeleteRequest("user","1");
        // 2. 执行请求   .delete
        DeleteResponse delete = client.delete(request, RequestOptions.DEFAULT);
        // 3. 解析结果
        System.out.println(delete.getResult());
    }
    /**
     * 初始化es的客户端
     */
    @Before
    public void init(){
         client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200))
        );
    }

    /**
     * 关闭客户端
     */
    @After
    public void close(){
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
